package org.i1510.common.utils.file;

import java.io.File;
import java.io.StringReader;

import org.apache.commons.betwixt.io.BeanReader;
import org.i1510.common.bean.SpringQuartzBean;
import org.i1510.common.bean.SpringQuartzBeans;
import org.i1510.common.bean.SpringQuartzPropertyBean;

/**
 * XML转Java
 */
public class ReadExampleApp {
	private static void addRules1(BeanReader d) {
		  // 当遇到<person>时，创建类Person的一个实例，并将其压入栈顶
		  d.addObjectCreate("beans/bean", SpringQuartzBean.class);
		  //修改标签 person 的属性class对应类属性classStr
		  d.addSetProperties("beans/bean", "class", "classStr");
		  
		  d.addSetProperties("beans/bean");
		  d.addSetNext("beans/bean", "addBean");

		  
		  d.addObjectCreate("beans/bean/property", SpringQuartzPropertyBean.class);
		  d.addSetNext("beans/bean/property", "addProperty");
		  d.addSetProperties("beans/bean/property");
		  
		  d.addCallMethod("beans/bean/property/value", "setValue", 0);
		  
		  d.addCallMethod("beans/bean/property/ref", "addRef", 1);
		  d.addCallParam("beans/bean/property/ref", 0, "bean");
		  
	
		 }
	

	public static final void main(String args[]) throws Exception {

		// 创建一个读取xml文件的流
		StringReader xmlReader = new StringReader(
				"<?xml version='1.0' ?><beans xmlns='http://www.springframework.org/schema/beans'><bean id='jobTask' class='show'>25</bean><bean id='jobTask1' class='show1'>26</bean></beans>");


		// 创建一个BeanReader实例，相当于转换器
		BeanReader beanReader = new BeanReader();

		// 配置BeanReader实例
		// beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
		// beanReader.getBindingConfiguration().setMapIDs(false); //不自动生成ID

		// 注册要转换对象的类，并指定根节点名称
//		beanReader.registerBeanClass("beans", SpringQuartzBeans.class);

		//创建xml对应beans
		SpringQuartzBeans springQuartzBeans = new SpringQuartzBeans();
		beanReader.push(springQuartzBeans);
		//增加规则
		addRules1(beanReader);
		beanReader.parse(new File("D://test.xml"));
		
		  
		  for(int i =0 ;i<springQuartzBeans.getBeans().size();i++){
			  SpringQuartzBean p = (SpringQuartzBean)springQuartzBeans.getBeans().get(i);
			  for(int j =0 ;j<p.getPropertys().size();j++){
				  SpringQuartzPropertyBean b = (SpringQuartzPropertyBean)p.getPropertys().get(j);
				  b.print();
			  }
		  }
		  
		 
		  

	
		// 将XML解析Java Object
//		SpringQuartzBeans springQuartzBeans = (SpringQuartzBeans) beanReader
//				.parse(xmlReader);

		// 在控制台输出对象
//		System.out.println(springQuartzBeans.getBeans());
	}
	
}